package com.bjpowernode.service.impl;

import com.bjpowernode.entity.Economics;
import com.bjpowernode.service.EconomicsService;
import com.bjpowernode.service.StatisticsService;

import java.util.*;

public class StatisticsServiceImpl implements StatisticsService {

    /**
     * 处理收支记录相关的业务
     */
    private EconomicsService economicsService;

    @Override
    public Integer selectBalanceOfMonth() {
        //取出当前月
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        int monthOfNow = calendar.get(Calendar.MONTH) + 1;

        //查询所有收支记录
        List<Economics> economics = this.economicsService.selectAllEconomics();

        //计算余额，+或-
        Integer balance = 0;

        for (Economics everyEconomics : economics) {
            Integer monthOfEconomic = this.getMonthOfEconomic(everyEconomics);

            if (monthOfEconomic == null) {
                return null;
            }

            //如果收支记录不是本月的
            if (!monthOfEconomic.equals(monthOfNow)) {
                continue;
            }

            if (everyEconomics.getMoney() == null) {
                continue;
            }

            //如果是支出记录，则相减
            if (everyEconomics.getConsumptionId() != null && everyEconomics.getIncomeId() == null) {
                balance = balance - everyEconomics.getMoney();
            } else if (everyEconomics.getIncomeId() != null && everyEconomics.getConsumptionId() == null) {
                //如果是收入记录，则相加
                balance = balance + everyEconomics.getMoney();
            } else {
                continue;
            }

        }
        return balance;
    }

    /**
     * 计算收支记录的月份
     *
     * @param economics
     * @return
     */
    private Integer getMonthOfEconomic(Economics economics) {
        //从收支记录日期字符串中截取
        //日期格式：yyyy-MM-dd，取两个-中间的字符

        String dateOfEconomic = economics.getDate();

        //如果收支记录的日期为空
        if (dateOfEconomic == null || dateOfEconomic.equals("")) {
            return null;
        }

        //从哪里开始截取
        int begin = dateOfEconomic.indexOf("-") + 1;

        //截取到
        int end = dateOfEconomic.lastIndexOf("-");

        //截取到的字符串
        String month = dateOfEconomic.substring(begin, end);

        return Integer.parseInt(month);
    }

    @Override
    public List<Economics> selectEconomicsOfCurrentMonth() {
        //取出当前月
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        int monthOfNow = calendar.get(Calendar.MONTH) + 1;

        //查询所有收支记录
        List<Economics> economics = this.economicsService.selectAllEconomics();

        for (Iterator<Economics> economicsIterator = economics.iterator(); economicsIterator.hasNext(); ) {
            Integer monthOfEconomic = this.getMonthOfEconomic(economicsIterator.next());

            if (!monthOfEconomic.equals(monthOfNow)) {
                economicsIterator.remove();
            }
        }

        return economics;
    }

    public EconomicsService getEconomicsService() {
        return economicsService;
    }

    public void setEconomicsService(EconomicsService economicsService) {
        this.economicsService = economicsService;
    }
}
